package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.dto.HisPatientDto;
import com.woniu.dto.HisRegisterDto;
import com.woniu.dto.PageDTO;
import com.woniu.moudle.HisRegister;
import com.woniu.mapper.HisRegisterMapper;
import com.woniu.param.*;
import com.woniu.service.HisRegisterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author w
 * @since 2021-11-11
 */
@Service
public class HisRegisterServiceImpl extends ServiceImpl<HisRegisterMapper, HisRegister> implements HisRegisterService {
    @Resource
    private HisRegisterMapper hisRegisterMapper;
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private StringRedisTemplate srt;
    @Override
    public HisRegisterDto newRegistration(AddHisRegisterParam addHisRegisterParam) {
        HisRegister hisRegister = new HisRegister();
        BeanUtils.copyProperties(addHisRegisterParam,hisRegister);
        int insert = hisRegisterMapper.insert(hisRegister);
        if(insert>0){
            try {
                String s = objectMapper.writeValueAsString(hisRegister);
                String key=hisRegister.getOfficeId()+"."+hisRegister.getDoctorId()+"."+hisRegister.getRegisterTime().substring(0,hisRegister.getRegisterTime().lastIndexOf(" "));
                srt.opsForZSet().add(key,s,hisRegister.getRegisterSort());
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

        }
        HisRegisterDto hisRegisterDto = new HisRegisterDto();
        BeanUtils.copyProperties(hisRegister,hisRegisterDto);
        return hisRegisterDto;
    }
    //TODD
//    @Override
//    public PageDTO<HisRegisterDto> pastRegistration(Integer patientId) {
//        QueryWrapper<HisRegister> hisRegisterQueryWrapper = new QueryWrapper<>();
//        hisRegisterQueryWrapper.eq("patient_id",patientId).eq("register_status",1);
//        List<HisRegister> list = list(hisRegisterQueryWrapper);
//        return null;
//    }

    @Override
    public HisRegisterDto modifyRegistrationStatus(Integer registerId, Integer registerStatus) {
        HisRegister hisRegister = new HisRegister();
        hisRegister.setRegisterId(registerId);
        hisRegister.setRegisterStatus(registerStatus);
        hisRegisterMapper.updateById(hisRegister);
        HisRegisterDto hisRegisterDto = new HisRegisterDto();
        BeanUtils.copyProperties(hisRegister,hisRegisterDto);
        return hisRegisterDto;
    }

    @Override
    public Boolean deleteRegistration(ArrayList<Integer> ids) {
        int i = hisRegisterMapper.deleteBatchIds(ids);
        return i>0?true:false;
    }

    @Override
    public HisRegisterDto modifyRegistration(ModifyHisRegisterParam modifyHisRegisterParam) {
        HisRegister hisRegister = new HisRegister();
        BeanUtils.copyProperties(modifyHisRegisterParam,hisRegister);
        hisRegisterMapper.updateById(hisRegister);
        HisRegisterDto hisRegisterDto = new HisRegisterDto();
        BeanUtils.copyProperties(hisRegister,hisRegisterDto);
        return hisRegisterDto;
    }

    @Override
    public PageDTO<HisRegisterDto> queryRegistration(ListHisRegisterParam listHisRegisterParam) {
        QueryWrapper<HisRegister> hisRegisterQueryWrapper = new QueryWrapper<>();
        if(listHisRegisterParam!=null){
            if(listHisRegisterParam.getDoctorId()!=null){
                hisRegisterQueryWrapper.eq("doctor_id",listHisRegisterParam.getDoctorId());
            }
            if(listHisRegisterParam.getDoctorName()!=null){
                hisRegisterQueryWrapper.eq("doctor_name",listHisRegisterParam.getDoctorName());
            }
            if(listHisRegisterParam.getOfficeId()!=null){
                hisRegisterQueryWrapper.eq("office_id",listHisRegisterParam.getOfficeId());
            }
            if(listHisRegisterParam.getOfficeName()!=null){
                hisRegisterQueryWrapper.eq("office_name",listHisRegisterParam.getOfficeName());
            }
            if(listHisRegisterParam.getPatientId()!=null){
                hisRegisterQueryWrapper.eq("patient_id",listHisRegisterParam.getPatientId());
            }
            if(listHisRegisterParam.getPatientName()!=null){
                hisRegisterQueryWrapper.eq("patient_name",listHisRegisterParam.getPatientName());
            }
            if(listHisRegisterParam.getRegisterId()!=null){
                hisRegisterQueryWrapper.eq("register_id",listHisRegisterParam.getRegisterId());
            }
            if(listHisRegisterParam.getRegisterPrice()!=null){
                hisRegisterQueryWrapper.eq("register_price",listHisRegisterParam.getRegisterPrice());
            }
            if(listHisRegisterParam.getRegisterSort()!=null){
                hisRegisterQueryWrapper.eq("register_sort",listHisRegisterParam.getRegisterSort());
            }
            if(listHisRegisterParam.getRegisterStatus()!=null){
                hisRegisterQueryWrapper.eq("register_status",listHisRegisterParam.getRegisterStatus());
            }
            if(listHisRegisterParam.getRegisterTime()!=null){
                hisRegisterQueryWrapper.eq("register_time",listHisRegisterParam.getRegisterTime());
            }
        }

        Page<HisRegister> page = page(new Page<HisRegister>(listHisRegisterParam.getPageParam().getPage(), listHisRegisterParam.getPageParam().getPageSize()), hisRegisterQueryWrapper);
        return PageDTO.bulid(page,HisRegisterDto.class);
    }

    @Override
    public PageDTO<HisRegisterDto> registerToday(TodayRegisterParam todayRegisterParam) {
        QueryWrapper<HisRegister> hisRegisterQueryWrapper = new QueryWrapper<>();
        hisRegisterQueryWrapper.eq("doctor_id",todayRegisterParam.getDoctorId()).eq("register_time",todayRegisterParam.getRegisterTime());
        Page<HisRegister> page = page(new Page<HisRegister>(todayRegisterParam.getPageParam().getPage(), todayRegisterParam.getPageParam().getPageSize()), hisRegisterQueryWrapper);

        return PageDTO.bulid(page,HisRegisterDto.class);
    }

    @Override
    public PageDTO<HisRegisterDto> patientPastRegistration(PastRegistrationParam pastRegistrationParamtientId) {

        QueryWrapper<HisRegister> hisRegisterQueryWrapper = new QueryWrapper<>();
        hisRegisterQueryWrapper.eq("patient_id",pastRegistrationParamtientId.getPatientId());
        Page<HisRegister> page = page(new Page<HisRegister>(pastRegistrationParamtientId.getPageParam().getPage(), pastRegistrationParamtientId.getPageParam().getPageSize()), hisRegisterQueryWrapper);

        return PageDTO.bulid(page,HisRegisterDto.class);
    }


}
